<template>
	<view class="login-container">
		<view class="login-header">
			<image src="/static/images/logo.png" class="logo" mode="aspectFit"></image>
			<text class="app-title">许可证管理系统</text>
			<text class="app-subtitle">PDA移动端</text>
		</view>
		
		<view class="login-form">
			<view class="form-item">
				<view class="input-wrapper">
					<text class="input-icon">👤</text>
					<input
						class="input-field"
						type="text"
						placeholder="请输入用户名"
						v-model="loginForm.username"
						:disabled="loading"
					/>
				</view>
			</view>

			<view class="form-item">
				<view class="input-wrapper">
					<text class="input-icon">🔒</text>
					<input
						class="input-field"
						:type="showPassword ? 'text' : 'password'"
						placeholder="请输入密码"
						v-model="loginForm.password"
						:disabled="loading"
					/>
					<text class="password-toggle" @click="togglePassword">
						{{ showPassword ? '👁️' : '👁️‍🗨️' }}
					</text>
				</view>
			</view>

			<view class="form-item">
				<view class="input-wrapper">
					<text class="input-icon">🏢</text>
					<picker 
						:value="departmentIndex" 
						:range="departmentList" 
						range-key="name"
						@change="onDepartmentChange"
						:disabled="loading"
					>
						<view class="picker-view">
							<text class="picker-text">{{ selectedDepartment.name || '请选择部门' }}</text>
							<text class="picker-arrow">▼</text>
						</view>
					</picker>
				</view>
			</view>
			
			<button 
				class="login-btn" 
				:class="{ 'loading': loading }"
				@click="handleLogin"
				:disabled="loading"
			>
				<text v-if="!loading">登录</text>
				<text v-else>登录中...</text>
			</button>
			
			<view class="login-tips">
				<text class="tips-text">请使用公司分配的账号登录</text>
			</view>
		</view>
		
		<view class="login-footer">
			<text class="footer-text">版本 1.0.0</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loading: false,
				showPassword: false,
				loginForm: {
					username: '',
					password: '',
					departmentId: ''
				},
				departmentIndex: 0,
				departmentList: [
					{ id: '1', name: '安全部' },
					{ id: '2', name: '生产部' },
					{ id: '3', name: '维修部' },
					{ id: '4', name: '工程部' },
					{ id: '5', name: '质量部' }
				]
			}
		},
		computed: {
			selectedDepartment() {
				return this.departmentList[this.departmentIndex] || {}
			}
		},
		onLoad() {
			// 检查是否已登录
			this.checkLoginStatus()
		},
		methods: {
			// 检查登录状态
			checkLoginStatus() {
				const token = uni.getStorageSync('token')
				if (token) {
					// 已登录，跳转到首页
					uni.reLaunch({
						url: '/pages/index/index'
					})
				}
			},
			
			// 切换密码显示
			togglePassword() {
				this.showPassword = !this.showPassword
			},
			
			// 部门选择变化
			onDepartmentChange(e) {
				this.departmentIndex = e.detail.value
				this.loginForm.departmentId = this.selectedDepartment.id
			},
			
			// 处理登录
			async handleLogin() {
				// 表单验证
				if (!this.validateForm()) {
					return
				}
				
				this.loading = true
				
				try {
					// 模拟登录API调用
					await this.loginApi()
					
					// 登录成功
					uni.showToast({
						title: '登录成功',
						icon: 'success'
					})
					
					// 保存登录信息
					uni.setStorageSync('token', 'mock_token_' + Date.now())
					uni.setStorageSync('userInfo', {
						username: this.loginForm.username,
						department: this.selectedDepartment.name,
						departmentId: this.loginForm.departmentId
					})
					
					// 跳转到首页
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/index/index'
						})
					}, 1500)
					
				} catch (error) {
					console.error('登录失败:', error)
					uni.showToast({
						title: '登录失败，请检查账号密码',
						icon: 'none'
					})
				} finally {
					this.loading = false
				}
			},
			
			// 表单验证
			validateForm() {
				if (!this.loginForm.username.trim()) {
					uni.showToast({
						title: '请输入用户名',
						icon: 'none'
					})
					return false
				}
				
				if (!this.loginForm.password.trim()) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					})
					return false
				}
				
				if (!this.loginForm.departmentId) {
					uni.showToast({
						title: '请选择部门',
						icon: 'none'
					})
					return false
				}
				
				return true
			},
			
			// 模拟登录API
			loginApi() {
				return new Promise((resolve, reject) => {
					setTimeout(() => {
						// 模拟登录验证
						if (this.loginForm.username === 'admin' && this.loginForm.password === '123456') {
							resolve({ success: true })
						} else {
							reject(new Error('用户名或密码错误'))
						}
					}, 2000)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.login-container {
		min-height: 100vh;
		background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
		display: flex;
		flex-direction: column;
		justify-content: center;
		padding: 40rpx;
	}
	
	.login-header {
		text-align: center;
		margin-bottom: 80rpx;
		
		.logo {
			width: 120rpx;
			height: 120rpx;
			margin-bottom: 30rpx;
		}
		
		.app-title {
			display: block;
			color: #fff;
			font-size: 48rpx;
			font-weight: bold;
			margin-bottom: 10rpx;
		}
		
		.app-subtitle {
			display: block;
			color: rgba(255, 255, 255, 0.8);
			font-size: 28rpx;
		}
	}
	
	.login-form {
		background: rgba(255, 255, 255, 0.95);
		border-radius: 24rpx;
		padding: 60rpx 40rpx;
		backdrop-filter: blur(10rpx);

		.form-item {
			margin-bottom: 40rpx;

			.input-wrapper {
				position: relative;
				display: flex;
				align-items: center;
				background: #f8f9fa;
				border-radius: 16rpx;
				padding: 0 20rpx;
				height: 100rpx;
				border: 2rpx solid transparent;
				transition: all 0.3s ease;

				&:focus-within {
					border-color: #409EFF;
					background: #fff;
					box-shadow: 0 0 0 4rpx rgba(64, 158, 255, 0.1);
				}

				.input-icon {
					font-size: 32rpx;
					margin-right: 20rpx;
					color: #666;
				}

				.input-field {
					flex: 1;
					font-size: 32rpx;
					color: #333;
					background: transparent;
					border: none;
					outline: none;
				}

				.password-toggle {
					font-size: 32rpx;
					color: #666;
					padding: 10rpx;
					cursor: pointer;
				}

				.picker-view {
					flex: 1;
					display: flex;
					align-items: center;
					justify-content: space-between;
					height: 100%;

					.picker-text {
						font-size: 32rpx;
						color: #333;
					}

					.picker-arrow {
						font-size: 24rpx;
						color: #999;
					}
				}
			}
		}

		.login-btn {
			width: 100%;
			height: 100rpx;
			background: linear-gradient(135deg, #409EFF 0%, #67C23A 100%);
			border-radius: 16rpx;
			border: none;
			color: #fff;
			font-size: 36rpx;
			font-weight: bold;
			margin-top: 40rpx;
			transition: all 0.3s ease;

			&:active {
				transform: scale(0.98);
			}

			&.loading {
				opacity: 0.7;
			}
		}

		.login-tips {
			text-align: center;
			margin-top: 30rpx;

			.tips-text {
				color: #666;
				font-size: 24rpx;
			}
		}
	}
	
	.login-footer {
		text-align: center;
		margin-top: 60rpx;
		
		.footer-text {
			color: rgba(255, 255, 255, 0.6);
			font-size: 24rpx;
		}
	}
</style>
